﻿using MongoDB.Bson;
using MongoDB.Driver;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Entities.Operate
{
    public class TokenForModifyUserMessage : Interface.ITokenForModifyUserMessage
    {
        private Data.IRepository<Entities.TokenForModifyUserMessage> tokenRepository = new Repository<Entities.TokenForModifyUserMessage>().GetRepository;

        public void Insert(Entities.TokenForModifyUserMessage param)
        {
            tokenRepository.Insert(param);
        }

        public void Delete(ObjectId Id)
        {
            tokenRepository.DeleteById(Id);
        }

        public Entities.TokenForModifyUserMessage GetById(ObjectId Id)
        {
            return tokenRepository.Get(Id);
        }

        public bool Update(Entities.TokenForModifyUserMessage args)
        {
            try
            {
                var fiter = Builders<Entities.TokenForModifyUserMessage>.Filter.Eq("Id", args.Id);
                IList<UpdateDefinition<Entities.TokenForModifyUserMessage>> update = new List<UpdateDefinition<Entities.TokenForModifyUserMessage>>();
                foreach (PropertyInfo p in args.GetType().GetProperties())
                {
                    if (!p.Name.Equals("Id"))
                        update.Add(Builders<Entities.TokenForModifyUserMessage>.Update.Set(p.Name, p.GetValue(args)));
                }
                tokenRepository.Update(fiter, Builders<Entities.TokenForModifyUserMessage>.Update.Combine(update));
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}
